package com.xxx.video.commen.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class TagLayout extends ViewGroup {

    private TagAdapter mAdapter;

    public TagLayout(Context context) {
        super(context);
    }

    public TagLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        int width = 0;
        int height = 0;

        // one line width
        int lineWidth = 0;

        //on line height
        int lineHeight = 0;

        //child count
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            //get child
            View childAt = getChildAt(i);

            //measure
            measureChild(childAt, widthMeasureSpec, heightMeasureSpec);

            //get margin
            MarginLayoutParams lp = (MarginLayoutParams) childAt.getLayoutParams();

            //get child width
            int childWidth = childAt.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;

            //get child height
            int childHeight = childAt.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            //if width > screen width to next line
            if (lineWidth + childWidth > widthSize) {
                //width
                width = Math.max(lineWidth, childWidth);
                lineWidth = childWidth;

                //height
                height += lineHeight;
                lineHeight = childHeight;
            } else {
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }

            //if this is last one
            if (i == childCount - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }

        //set width and height
        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? widthSize : width
                , (modeHeight == MeasureSpec.EXACTLY) ? heightSize : height
        );
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int lineWidth = 0;
        int lineHeight = 0;
        int left = 0, top = 0;
        int measuredWidth = getMeasuredWidth();
        for (int i = 0; i < childCount; i++) {
            //get child
            View childView = getChildAt(i);
            //if the child  visibility gone
            if (childView.getVisibility() == GONE) {
                continue;
            }
            MarginLayoutParams lp = (MarginLayoutParams) childView
                    .getLayoutParams();
            int childWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (lineWidth + childWidth > measuredWidth) {
                //if next line，height plus，left start zero
                top += lineHeight;
                left = 0;
                //init lineHeight and lineWidth
                lineHeight = childHeight;
                lineWidth = childWidth;
            } else {
                lineHeight = Math.max(lineHeight, childHeight);
                lineWidth += childWidth;
            }
            //computer view
            int cl = left + lp.leftMargin;
            int ct = top + lp.topMargin;
            int cr = cl + childView.getMeasuredWidth();
            int cb = ct + childView.getMeasuredHeight();

            //set child layout
            childView.layout(cl, ct, cr, cb);

            //update left value,take the next child layout to the begin
            left += childWidth;
        }
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }


    /**
     * set adapter
     *
     * @param adapter adapter
     */
    public void setAdapter(TagAdapter adapter) {
        if (adapter == null) {
            throw new NullPointerException("TagAdapter can not empty");
        }
        removeAllViews();
        mAdapter = adapter;
        int itemCount = mAdapter.getItemCount();
        for (int i = 0; i < itemCount; i++) {
            View childView = mAdapter.getView(i, this);
            addView(childView);
        }
    }
}
